package com.bhs.sansonglogistics.ui.order;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.OrderGoodsBean;
import com.bhs.sansonglogistics.bean.OrderGoodsDataBean;
import com.bhs.sansonglogistics.dialog.MyClickListener;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.OrderGoodsAdapter;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.ArrayList;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 选择专线订单
 */
public class SelectOrderActivity extends BaseActivity implements NetCallBack {


    private TextView mTvTitle;
    private RecyclerView mRvList;
    private CheckBox mCbAll;
    private TextView mTvSelected;
    private Button mBtnSubmit;
    private int line_id;
    private OrderGoodsAdapter adapter;
    private String orders;
    ArrayList<OrderGoodsBean> deleteOrder;

    @Override
    public int initLayout() {
        return R.layout.activity_select_order;
    }

    @Override
    public void initView() {
        line_id = getIntent().getIntExtra("line_id", 0);
        orders = getIntent().getStringExtra("orders");
        deleteOrder = getIntent().getParcelableArrayListExtra("deleteOrder");
        mTvTitle = findViewById(R.id.tv_title);
        mRvList = findViewById(R.id.rv_list);
        mCbAll = findViewById(R.id.cb_all);
        mTvSelected = findViewById(R.id.tv_selected);
        mBtnSubmit = findViewById(R.id.btn_submit);
        mTvTitle.setText(getIntent().getStringExtra("reach"));
        mBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StringBuilder stringBuilder = new StringBuilder();
                ArrayList<OrderGoodsBean> item = new ArrayList<>();
                for (OrderGoodsBean datum : adapter.getData()) {
                    if (datum.isChecked()) {
                        stringBuilder.append(datum.getDeliver_sn());
                        stringBuilder.append(",");
                        item.add(datum);
                    }
                }
                if (TextUtils.isEmpty(stringBuilder)) {
                    ToastUtil.show("请选择一个订单");
                    return;
                }
                stringBuilder = new StringBuilder(stringBuilder.substring(0, stringBuilder.toString().lastIndexOf(',')));
                Intent i = new Intent();
                i.putExtra("orders", stringBuilder.toString());
                i.putExtra("ordersInfo", item);
                setResult(3, i);
                finish();
            }
        });
        mCbAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                for (OrderGoodsBean datum : adapter.getData()) {
                    if (datum.getCancel_status() == 0) {
                        datum.setChecked(b);
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void initData() {
        networkRequest(netApi.getSpecialLineOrderList(100, line_id), this);
        adapter = new OrderGoodsAdapter(2);
        mRvList.setAdapter(adapter);
        mRvList.setLayoutManager(new LinearLayoutManager(this));
        adapter.setMyClickListener(new MyClickListener() {
            @Override
            public void onClick(View v) {
                int number = 0;
                for (OrderGoodsBean datum : adapter.getData()) {
                    if (datum.isChecked()) {
                        number++;
                    }
                }
                mTvSelected.setText(String.format("已选%s单", number));
            }
        });
        adapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.layout_empty, null));
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        OrderGoodsDataBean dataBean = new Gson().fromJson(json, OrderGoodsDataBean.class);
        if (dataBean.isStatus()) {
            int number = 0;
            if (!MyUtils.isEmpty(deleteOrder)) {
                for (OrderGoodsBean orderGoodsBean : deleteOrder) {
                    if (orderGoodsBean.isCurrent()) {
                        dataBean.getData().getList().add(orderGoodsBean);
                    }
                }
            }
            if (!TextUtils.isEmpty(orders)) {
                String[] orderNumber = orders.split(",");
                for (OrderGoodsBean orderGoodsBean : dataBean.getData().getList()) {
                    for (String s : orderNumber) {
                        if (orderGoodsBean.getDeliver_sn().equals(s)) {
                            number++;
                            orderGoodsBean.setChecked(true);
                        }
                    }
                }
            }
            mTvSelected.setText(String.format("已选%s单", number));
            adapter.setNewData(dataBean.getData().getList());

        } else {
            ToastUtil.show(dataBean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}